﻿using ProjectSemIII.Administrator.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Support
{
    public partial class FAQ : System.Web.UI.Page
    {
        private FAQSupportProcess process;
        private const bool blnAllowPaging = true;
        private const int iPageSize = 5;
        private static PagedDataSource pgdProducts;
        private List<Entity.FAQ> listFAQ;
        protected void Page_Load(object sender, EventArgs e)
        {
            process = new FAQSupportProcess();
            pgdProducts = new PagedDataSource();
            if(!Page.IsPostBack){
                this.getAllFAQ();
            }
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            if (this.validateFormInfomation())
            {
                Entity.FAQ faq = new Entity.FAQ();
                faq.Question = txtQuestion.Text;
                faq.Answer = txtAnswer.Text;
                faq.FAQCatalog = Convert.ToInt32(ddFAQCatalog.SelectedValue);
                int result = process.createNewFAQ(faq);
                if (result == 10)
                {
                    lbError.Text = "Create new FAQ successful!";
                    this.getAllFAQ();
                }
                else
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Error in create new FAQ!";
                }
            }
        }
        private bool validateFormInfomation()
        {
            bool flag = true;
            if (txtQuestion.Text == "" || txtQuestion.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the question!";
                flag = false;
            }
            else if (txtAnswer.Text == "" || txtAnswer.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the answer!";
                flag = false;
            }
            else if (Convert.ToInt32(ddFAQCatalog.SelectedValue) == -1)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please choose FAQ Catalog!";
                flag = false;
            }
            else if (process.checkQuestionTitle(Convert.ToInt32(ddFAQCatalog.SelectedValue), txtQuestion.Text) == true)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "The same question is exits!";
                flag = false;
            }
            return flag;
        }
        protected void ddFAQCatalog_SelectedIndexChanged(object sender, EventArgs e)
        {
            nameChoose.InnerText = ddFAQCatalog.SelectedItem.Text;
        }

        protected void btPrev_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            if (lbuton.ID == "btPrev")
            {
                if (ThisPageNumber > 0)
                {
                    ThisPageNumber = ThisPageNumber - 1;
                    this.getAllFAQ();
                }
            }
            //Change page when user click to next number button
            else if (lbuton.ID == "btNext")
            {
                if (ThisPageNumber < totalPage.Items.Count - 1)
                {
                    ThisPageNumber = ThisPageNumber + 1;
                    this.getAllFAQ();
                }
            }
        }

        protected void PageNumber_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            ThisPageNumber = Convert.ToInt32(lbuton.Text) - 1;
            this.getAllFAQ();
        }

        protected void allFAQ_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int idFAQ = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Delete")
            {
                int result = process.deleteFAQByFAQID(idFAQ);
                if(result == 10){
                    lbError.Text = "Delete FAQ successful!";
                    this.getAllFAQ();
                }
                else
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Error in delete FAQ process!";
                }
            }
            else if (e.CommandName == "Edit")
            {
                ClientScript.RegisterStartupScript(GetType(), "openEditSurveyPage", "window.open('EditFAQ.aspx?faqid=" + idFAQ + "');", true);
            }
        }

        private void getAllFAQ()
        {
            listFAQ = process.getAllFAQ();

            pgdProducts.AllowPaging = blnAllowPaging;
            pgdProducts.PageSize = iPageSize;
            pgdProducts.DataSource = listFAQ;

            pgdProducts.CurrentPageIndex = ThisPageNumber;
            fillTotalPage(pgdProducts.PageCount);

            allFAQ.DataSource = pgdProducts;
            allFAQ.DataBind();
        }

        private void fillTotalPage(int iTotalPages)
        {
            List<int> pageID = new List<int>();
            for (int i = 1; i <= iTotalPages; i++)
            {
                pageID.Add(i);
            }
            totalPage.DataSource = pageID;
            totalPage.DataBind();
        }

        private int ThisPageNumber
        {
            get
            {
                object obj = ViewState["ThisPageNumber"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                this.ViewState["ThisPageNumber"] = value;
            }
        }
    }
}